<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/16
 * Time: 14:31
 */
namespace app\api\controller;

use think\Controller;
use app\reserve\model\Order as OrderModel;
use think\Db;
use think\Request;

class Diagram extends Controller{

    // 商户订单模型
    private $order;

    public function __construct()
    {
        $this->order = new OrderModel();
    }

    /**
     * 商户订单曲线图
     * User: lijie
     * Date: 2018/10/16
     */
    public function orderCash(Request $request)
    {
        $data = array();
        $arrX    = array();
        $arrY    = array();
        $param = $request->post();
        // $where['a.status'] = 4;
        // 是否是商户查看曲线
        if (!empty(input('post.a_id'))){
            $where['a.c_id'] = input('post.a_id');
        }
        // 昨天曲线图
        if(!empty($param["yestoday"])) {
            $starttime = $param["yestoday"] . " 00:00:00";
            $endtime = $param["yestoday"] . " 23:59:59";
            $where["a.ctime"] = array(array('EGT', strtotime($starttime)), array('ELT', strtotime($endtime)), 'and');
        }else{
            $day            = time();
//            $day            = strtotime(date("Y-m-d",time())." 00:00:00");
            $where['a.ctime'] = array("LT",$day);
        }
        // 总订单条数
        $data['total'] = Db::name('order')->alias("a")->where($where)->count();
        // 格式化ctime
        $sql = Db::name('order')->field("id,c_id,status,FROM_UNIXTIME(ctime,'%Y-%m-%d') group_time,ctime")->buildSql();
        $res = Db::table("{$sql}")->alias("a")->where($where)->field("a.group_time,count(id) total,a.status,a.ctime")->group("a.group_time")->select();
        // 输出格式
        foreach ($res as $k=>$v) {
            $arrX[] = $v['group_time'];
            $arrY[] = $v['total'];
        }
        $data['list']['X'] = $arrX;
        $data['list']['Y'] = $arrY;
        return apiResponse(200,"获取成功",$data);
    }

    /**
     * 洗车商户订单曲线图
     * User: lijie
     * Date: 2018/10/16
     */
    public function orderWash(Request $request)
    {
        $data = array();
        $arrX    = array();
        $arrY    = array();
        $param = $request->post();
        // $where['a.status']    = 3;
        // 商户查看曲线图
        if (!empty(input('post.a_id'))){
            $where['a.a_id']  = input('post.a_id');
        }
        // 洗车商户查看曲线图
        if (!empty(input('post.wash_id'))){
            $where['a.wash_id'] = input('post.wash_id');
        }
        // 昨天曲线图
        if(!empty($param["yestoday"])) {
            $starttime = $param["yestoday"] . " 00:00:00";
            $endtime = $param["yestoday"] . " 23:59:59";
            $where["a.ctime"] = array(array('EGT', strtotime($starttime)), array('ELT', strtotime($endtime)), 'and');

        }else{
            $day            = time();
//            $day            = strtotime(date("Y-m-d",time())." 00:00:00");
            $where['a.ctime'] = array("LT",$day);
        }
        // 总条数
        $data['total'] = Db::name('orderwash')->alias("a")->where($where)->count();
        // 格式化ctime
        $sql = Db::name("orderwash")->field("id,a_id,wash_id,status,FROM_UNIXTIME(ctime,'%Y-%m-%d') group_time,ctime")->buildSql();
        // 分组查询
        $res = Db::table("{$sql}")->alias("a")->where($where)->field("a.group_time,count(id) total")->group("a.group_time")->select();
        // 整理输出格式
        foreach ($res as $k=>$v) {
            $arrX[] = $v['group_time'];
            $arrY[] = $v['total'];
        }
        $data['list']['X'] = $arrX;
        $data['list']['Y'] = $arrY;
        return apiResponse(200,'获取成功',$data);
    }
}